package com.zis.wechat.service.impl;

import com.alibaba.fastjson.JSON;
import com.zis.starter.core.Constant;
import com.zis.starter.core.ResponseEntity;
import com.zis.starter.core.SearchParams;
import com.zis.wechat.dao.TUserDao;
import com.zis.wechat.model.TUser;
import com.zis.wechat.service.ITUserInter;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 微信用户信息
 * Create by wangshen 2018/7/18
 */
@Service
@Transactional("txManager")
public class TUserInterImpl implements ITUserInter {


    @Autowired
    private TUserDao userDao;


    /**
     * 新增
     * @param user
     * @return
     * @throws Exception
     */
    @Override
    public ResponseEntity insert(TUser user) throws Exception {
        int num = userDao.getSQLManager().insert(user);
        if (num > 0) {
            return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_FAIL);
    }

    /**
     * 更新
     * @param user
     * @return
     * @throws Exception
     */
    @Override
    public ResponseEntity update(TUser user) throws Exception {
        int num = userDao.getSQLManager().updateTemplateById(user);
        if (num > 0) {
            return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_FAIL);
    }

    /**
     * 根据ID删除
     * @param tuId
     * @return
     * @throws Exception
     */
    @Override
    public ResponseEntity delete(String tuId) throws Exception {
        int num = userDao.getSQLManager().deleteById(TUser.class, tuId);
        if (num > 0) {
            return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_FAIL);
    }

    /**
     * 根据ID查询
     * @param tuId
     * @return
     * @throws Exception
     */
    @Override
    public ResponseEntity selectById(String tuId) throws Exception {
        TUser tUser = userDao.getSQLManager().unique(TUser.class, tuId);
        if (tUser != null) {
            return ResponseEntity.success(JSON.parseObject(JSON.toJSONString(tUser)));
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_NULL);
    }

    /**
     * 分页查询
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public PageQuery<TUser> findByPage(SearchParams params) throws Exception {
        PageQuery<TUser> query = new PageQuery<>();
        query.setPageNumber(params.getPage());
        query.setPageSize(params.getPageSize());
        query.setParas(params.getParams());
        userDao.getSQLManager().pageQuery("wechat.wechatUser.query", TUser.class, query);
        return query;
    }

    /**
     * 条件查询
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public List<TUser> findByCondition(Map params) throws Exception {
        List<TUser> resultList = userDao.getSQLManager().select("wechat.wechatUser.query", TUser.class, params);
        return resultList;
    }


}
